package com.sign2024.sign2024project.signBase.ExcelType;

import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.sign2024.sign2024project.signBase.vo.WzBaseVo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 自定义写处理器，实现单元格合并
public class WzSignBaseHandler extends AbstractRowWriteHandler {
    private final List<WzBaseVo> dataList;
    //    private final Map<String, Integer> mergeMap = new HashMap<>();
    // 技术分组
    private final Map<String, Integer> skillClassMap = new HashMap<>();

    private final Map<String, Integer> bossClassMap = new HashMap<>();

    public WzSignBaseHandler(List<WzBaseVo> dataList) {
        this.dataList = dataList;
        calculateMergeRanges();
    }

    int count = 0;

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        if (isHead) {
            return;
        }

        // 技术分组
        String skillClassValue = dataList.get(relativeRowIndex).getSkillClass();
        if (skillClassValue != null && skillClassMap.containsKey(skillClassValue)) {
            int firstRow = findFirstRowForMerge(skillClassValue, dataList, "skillClass");
            int lastRow = firstRow + skillClassMap.get(skillClassValue) - 1;
            if (lastRow > firstRow) {
                if (relativeRowIndex == lastRow - 1) {
                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 17, 17));
                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 18, 18));
                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 19, 19));
                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 20, 20));
                }
            }
        }

        // 商务分组
        String bossClassValue = dataList.get(relativeRowIndex).getBossClass();
        if (bossClassValue != null && bossClassMap.containsKey(bossClassValue)) {
            int firstRow = findFirstRowForMerge(bossClassValue, dataList, "bossClass");
            int lastRow = firstRow + bossClassMap.get(bossClassValue) - 1;
            if (lastRow > firstRow) {
                if (relativeRowIndex == lastRow - 1) {
                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 21, 21));
                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 22, 22));
                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 24, 24));
                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 26, 26));
                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 27, 27));
                }
            }
        }
    }

    private void calculateMergeRanges() {
        for (int i = 0; i < dataList.size(); i++) {
            WzBaseVo item = dataList.get(i);
            if (item.getSkillClass() != null) {
                skillClassMap.put(item.getSkillClass(), skillClassMap.getOrDefault(item.getSkillClass(), 0) + 1);
            }
            if (item.getBossClass() != null) {
                bossClassMap.put(item.getBossClass(), bossClassMap.getOrDefault(item.getBossClass(), 0) + 1);
            }
        }
    }

    private int findFirstRowForMerge(String value, List<WzBaseVo> dataList, String property) {
        for (int i = 0; i < dataList.size(); i++) {
            WzBaseVo item = dataList.get(i);
            if (property.equals("skillClass") && value.equals(item.getSkillClass())) {
                return i + 1; // +1 因为行号从0开始
            } else if (property.equals("bossClass") && value.equals(item.getBossClass())) {
                return i + 1;
            }
        }
        return -1;
    }
}